const zlib = require("zlib");
const fs = require("fs");

// 压缩方式：
// - gzip
// - deflate

// gzip
{
  // 压缩
  // const readStream = fs.createReadStream("test.txt");
  // const writeStream = fs.createWriteStream("test.txt.gz");
  // readStream.pipe(zlib.createGzip()).pipe(writeStream);
  // 解压
  // const readStream2 = fs.createReadStream("test.txt.gz");
  // const writeStream2 = fs.createWriteStream("unzip1.txt");
  // readStream2
  //   .pipe(zlib.createGunzip())
  //   .pipe(writeStream2)
  //   .on("error", (err) => {
  //     console.error("解压过程中发生错误:", err);
  //   })
  //   .on("finish", () => {
  //     console.log("解压完成");
  //   });
}

// deflate
{
  // 压缩
  // const readStream = fs.createReadStream("test.txt");
  // const writeStream = fs.createWriteStream("test2.txt.deflate");
  // readStream.pipe(zlib.createDeflate()).pipe(writeStream);

  // 解压
  const readStream2 = fs.createReadStream("test2.txt.deflate");
  const writeStream2 = fs.createWriteStream("unzip2.txt");
  readStream2
    .pipe(zlib.createInflate())
    .pipe(writeStream2)
    .on("error", (err) => {
      console.error("解压过程中发生错误:", err);
    })
    .on("finish", () => {
      console.log("解压完成");
    });
}
